package arrayList_basics.src.StudentSystem;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class index {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
            Student s1 = new Student("111","张三",17,"北京");
            Student s2 = new Student("222","李四",18,"上海");
            Student s3 = new Student("333","王五",19,"广州");
            students.add(s1);
            students.add(s2);
            students.add(s3);
       loop: while (true){
            System.out.println("-------------欢迎来到学生管理系统----------------");
            System.out.println("1：添加学生");
            System.out.println("2：删除学生");
            System.out.println("3：修改学生");
            System.out.println("4：查询学生");
            System.out.println("5：退出");
            System.out.println("请输入您的选择:");
            Scanner sc = new Scanner(System.in);
            String choice = sc.next();
            switch (choice) {
                case "1": handelAdd(students); break;
                case "2": handelDelete(students); break;
                case "3": handelSet(students); break;
                case "4": handelGet(students); break;
                case "5": break loop; // 循环嵌套时候给外层whie循环起名字 表示跳出外循环System.exit(0);停止虚拟机运行
            }
        }
    }

    public static void handelAdd(ArrayList<Student> students) {
        Random rand = new Random();
        Scanner sc = new Scanner(System.in);
        Student s = new Student();
        System.out.println("输入姓名");
        s.setName(sc.next());
        System.out.println("输入年龄");
        s.setAge(sc.nextInt());
        System.out.println("输入地址");
        s.setSite(sc.next());
        s.setId( String.valueOf(rand.nextInt()));
        students.add(s);
    };
    public static void handelDelete(ArrayList<Student> students) {
        Scanner sc = new Scanner(System.in);
        System.out.println("删除哪一个学生");
        int index = getIndex(students);
        if (index>=0){
            students.remove(index);
            System.out.println("删除成功");
            return;
        } else {
            System.out.println("没有学生信息");
            return;
        }
    }
    public static void handelSet(ArrayList<Student> students) {
        System.out.println("修改哪一个学生");
        Scanner sc = new Scanner(System.in);
        int index = getIndex(students);
        if (index >=0) {
            System.out.println("修改姓名");
            students.get(index).setName(sc.next());
            System.out.println("修改年龄");
            students.get(index).setAge(sc.nextInt());
            System.out.println("修改地址");
            students.get(index).setSite(sc.next());
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败学生不存在");
        }
    }
    public static void handelGet(ArrayList<Student> students) {
        if (students. isEmpty()){
            System.out.println("信息为空");
          return;
        }
        for (int i = 0; i < students.size(); i++) {
            System.out.println(students.get(i).getId()+','+students.get(i).getName()+','+students.get(i).getAge()+','+students.get(i).getSite());
        }

    }
    public static int getIndex(ArrayList<Student> students){
        int index = -1;
        Scanner sc = new Scanner(System.in);
        String item = sc.next();
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId().equals(item)){
                index = i;
            }
        }
        return index;

    }
}
